<?php
    define('CONFIG_FILE',true);
    include '../config.php';
    
    define('DB_FILE',true);
    require_once '../Tool/DB/SqlDBManager.class.php';

    session_start();
    if (empty($_SESSION["userType"])) {
        $url="../Index.php";
        include_once '../Tool/Common/Redirect.php';
        exit();
    }

    if($_SESSION["userType"]!="System Admin"){
        $url="../Index.php";
        include_once '../Tool/Common/Redirect.php';
        exit();
    }

    $CentreName = $_REQUEST['InsertCentreName'];
    $Address = $_REQUEST['InsertAddress'];
    $PostalCode = $_REQUEST['InsertPostalCode'];

    $sqlDBManager = new SqlDBManager();

    $sql = "insert into centre(CentreName,Address,PostalCode) values(?,?,?)";
    $parameters = array($CentreName,$Address,$PostalCode);
    $res=$sqlDBManager->execute_dml($sql,$parameters);

    if ($res>0){
        $sql = "Select CentreId from Centre where CentreName=?";
        $parameters = array($CentreName);
        $res=$sqlDBManager->queryRow($sql,$parameters);
        if(!empty($res)){
            $centreId = $res['CentreId'];
        }
        $todayEndDate = date('Y-m-t', strtotime(date('d-m-Y')));
        $sql = "insert into tracking(CentreId, Date,Status)VALUES(?,?,?)";
        $parameters = array($centreId,$todayEndDate,'Not Exported');
        $res=$sqlDBManager->execute_dml($sql,$parameters);

    	echo json_encode(array('success'=>true));	
    } else {
    	echo json_encode(array('msg'=>'Create new centre failed due to database error.'));
    }

     //close connection
    $sqlDBManager->close_connect();
?>